package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;

import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    //查找所有信息
    @RequestMapping("/findAll")
    public Result findAll() {

        try {
            List<Menu> list= menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }


    //查找菜单信息
/*
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        //PageResult pageResult=null;
        try {
            List<Menu>  pageResult= menuService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
*/
    //查找菜单信息
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult=null;
        try {
            pageResult= menuService.findPage(queryPageBean);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }


    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try{
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL+","+e.getMessage());
        }
    }

    @RequestMapping("/findAllMenu")
    public Result findAllMenu() {
        try {
            List<Map<String, Object>> list = menuService.findAllMenuIds();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, list);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //根据用户id查询用户信息
    @RequestMapping("/findById")
    public Result findById(Integer menuId) {

        try{
           Menu menu = menuService.findById(menuId);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    //更新菜单信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL+","+e.getMessage());
        }
        return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {

        try{
            menuService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL+","+e.getMessage());
        }
    }
    @RequestMapping("/findByVal")
    public Result findByVal(Integer val) {

        try{
           String path = menuService.findByVal(val);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS,path);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

}
